package org.security.config;


import org.pmw.tinylog.Logger;
import org.security.filter.FilterHandlerManager;
import org.security.filter.FilterHandlerProxy;
import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author jt
 * @version 1.0
 * @date 2024/6/25 17:09
 */
@Configuration
@ConditionalOnClass
@EnableConfigurationProperties(FreeProperties.class)
@ComponentScan(value = {"org.security"})
public class FreeAutoConfiguration implements ApplicationContextAware {

    private ApplicationContext context;

    private FreeProperties freeProperties;

    @Bean
    public FilterRegistrationBean<FilterHandlerProxy> functionFilter() {
        Logger.info("building {}"+FilterHandlerProxy.class);
        FilterRegistrationBean<FilterHandlerProxy> registration = new FilterRegistrationBean<FilterHandlerProxy>();
        FilterHandlerProxy handlerProxy = new FilterHandlerProxy(freeProperties,context.getBean(FilterHandlerManager.class));
        registration.setFilter(handlerProxy);
//        registration.addUrlPatterns(urlPatterns);
        registration.setName("handlerProxy");
        registration.setOrder(0);
        return registration;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}
